<import resource="classpath:alfresco/extension/templates/webscripts/vgr/common.js">

createFolders();
vgr = companyhome.childByNamePath("vgr");
savedSearchesFolder = vgr.childByNamePath("Saved Searches");
previewFolder = savedSearchesFolder.childByNamePath("preview");

// values from the form
// Information about the list
listType = args["list-type"];
listName = args["list-name"];
listDescription = args["list-description"];
listAdministration = args["list-administration"];
listMajorArea = args["list-majorarea"];
// Search conditions
searchDocType = getValue("search-doctype");
searchDocTitle = getValue("search-doctitle");
searchDocId = getValue("search-docid");
searchDocCreatedBy = getValue("search-doccreatedby");
searchDocDescription = getValue("search-docdescription");

searchCreatedbyAuthor = getValue("search-createdbyauthor");
searchCreatedbyAdministration = getValue("search-createdbyadministration");
searchCreatedByUnit = getValue("search-createdbyunit");

searchResponsibleName = getValue("search-responsiblename");
searchResponsibleAdministration = getValue("search-responsibleadministration");
searchResponsibleUnit = getValue("search-responsibleunit");

searchBusinessMajorArea = getValue("search-businessmajorarea");
searchBusinessMinorArea = getValue("search-businessminorarea");
searchBusinessProject = getValue("search-businessproject");
searchBusinessAssigner = getValue("search-businessassigner");

searchReferenceDocumentSuite = getValue("search-referencedocumentsuite");
searchReferenceNumber = getValue("search-referencenumber");

// Display options
versionAll = (args["version-all"] == "on") ? true:false;
columnDocId = (args["column-docid"] == "on") ? true:false;
columnDocType = (args["column-doctype"] == "on") ? true:false;
columnAuthor = (args["column-author"] == "on") ? true:false;
columnVersion = (args["column-version"] == "on") ? true:false;
columnResponsibleName = (args["column-responsiblename"] == "on") ? true:false;
columnPublishedAt = (args["column-publishedat"] == "on") ? true:false;

showIcon = (args["more-showicon"] == "on") ? true:false;
showDescription = (args["more-showdescription"] == "on") ? true:false;

sortColumn = args["sort-column"];
sortDesc = (args["sort-desc"] == "on") ? true:false;

accessZone = args["accesszone"];

// end of values from the form

// Validation
if (!valid_list_name(listName)) {
	status.code = 500;
	status.message = "S&ouml;kningen kan inte sparas p&aring; grund av ett ogiltigt namn på listan.";
	status.redirect = true;
}

checkListName = savedSearchesFolder.childByNamePath(listName);
if ((checkListName != null) && (args["preview"] == undefined)) {
	status.code = 500;
	status.message = "En sparad s&ouml;kning med namnet " + listName + " finns redan.";
	status.redirect = true;
}
// End of validation

if (args["preview"] == undefined) {
	savedSearch = savedSearchesFolder.createFile(listName);
//if preview
} else {
	listName += "_preview";
	file = previewFolder.childByNamePath(listName);
	if (file != null) file.remove();
	
	savedSearch = previewFolder.createFile(listName);
}

// Sets the values from the form and creates a new Saved Search.
savedSearch.specializeType("vgr:savedSearch");
//savedSearch.addAspect("cm:versionable");

savedSearch.properties["vgr:listType"] = listType;
savedSearch.properties["vgr:listName"] = listName;
savedSearch.properties["vgr:listDescription"] = listDescription;
savedSearch.properties["vgr:listAdministration"] = listAdministration;
savedSearch.properties["vgr:listMajorArea"] = listMajorArea;

savedSearch.properties["vgr:searchCriterias"] = new Array(searchDocType, searchDocTitle, searchDocId, searchDocCreatedBy,
		searchDocDescription, searchCreatedbyAuthor, searchCreatedbyAdministration, searchCreatedByUnit, searchResponsibleName, 
		searchResponsibleAdministration, searchResponsibleUnit, searchBusinessMajorArea, searchBusinessMinorArea,
		searchBusinessProject, searchBusinessAssigner, searchReferenceDocumentSuite, searchReferenceNumber);

savedSearch.properties["vgr:showAllVersions"] = versionAll;
savedSearch.properties["vgr:showIcon"] = showIcon;
savedSearch.properties["vgr:showDescription"] = showDescription;
savedSearch.properties["vgr:showSortBy"] = sortColumn;
savedSearch.properties["vgr:showDescSort"] = sortDesc;
savedSearch.properties["vgr:searchAccessZone"] = accessZone;

columns = new Array();
if (columnDocId) { columns.push("vgr:docId"); }
if (columnDocType) { columns.push("vgr:docType"); }
if (columnVersion) { columns.push("cm:versionLabel"); }
if (columnAuthor) { columns.push("vgr:createdByAuthor") }
if (columnResponsibleName) { columns.push("vgr:responsibleName"); }
if (columnPublishedAt) { columns.push("vgr:publishedAt"); }
savedSearch.properties["vgr:showColumns"] = columns;

savedSearch.save();

model.listName = listName;